
var zNodes;
var d;
var root;
var history;
var forward;
var treeInitComplete = false;
$(function () {
	loadFolders();
	$('#ftpId').attr('class','ftp current');
	$("#action_refresh").click(function () {
		loadFolders();
	});
	$("#action_delete").each(function () {
		$(this).click(function () {
			var size = $(".main_table .selected").size();
			if (size > 0) {
				$("#actionForm").ajaxSubmit({url:"../ftp/delete.json", type:"post", dataType:"json", success:function (data) {
						if (data.success) {
							$(".main_table .selected").each(function () {
								$(this).remove();
							});
						} else {
							Ext.dialog({title:"Delete Failed", height:200, html:data.message, buttons:[{name:"Ok", handle:function () {
								Ext.hideDialog();
							}}]});
						}
				}});
			} else {
				alert('请选择要删除的文件');
			}
		});
	});
	$("#action_reply").click(function () {
		if ($("#absolutePath").val() != "/") {
			forward = $("#absolutePath").val();
			loadFiles(history);
			enabeldForward();
		}
	});
	$("#action_forward").click(function () {
		loadFiles(forward);
		disabeldForward();
	});
	$("#folder_remove").click(function () {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0];
		if (nodes.length == 0) {
			alert('请选择要删除的文件夹');
			return;
		} else {
			var current = $("#treeDemo li .curSelectedNode");
			if ($(current).attr("title") == "根目录") {
				Ext.dialog({title:"Information", height:200, html:"Cannot delete root folder.", buttons:[{name:"Ok", handle:function () {
					Ext.hideDialog();
				}}]});
				return;
			}
		}
		$("#absolutePath").val($(".curSelectedNode input").get(0).value);
			$("#actionForm").ajaxSubmit({url:"delete.json", type:"post", dataType:"json", success:function (data) {
				if (data.success) {
					zTree.removeNode(treeNode);
				} else {
					Ext.dialog({title:"Delete Failed", height:200, html:data.message, buttons:[{name:"Ok", handle:function () {
						Ext.hideDialog();
					}}]});
				}
			}});
	});
});
function beforeDrag(treeId, treeNodes) {
	return false;
}
function beforeRename(treeId, treeNode, newName) {
	if (newName.length == 0) {
		alert("Folder name cannot be null.");
		return false;
	} else {
		for (var i = 0; i < zNodes.length; i++) {
			if (zNodes[i].pId == treeNode.pId && zNodes[i].name == newName) {
				alert("Folder has already exist.please use other folder name.");
				return false;
			}
		}
		$("#bean_name").val(treeNode.name);
		$("#actionForm").ajaxSubmit({url:"../ftp/rename.json", type:"post", dataType:"json", success:function (data) {
		}});
	}
	return true;
}
var newCount = 1;
function add(e) {
	$('#folder_input').val('');
	$('#folder_edit_dialog').dialog({
		width:300,height:130,title:"新建文件夹",
		buttons:{
			'保存':function(){
				$('#bean_name').val($('#folder_input').val());
				$("#actionForm").ajaxSubmit({
					url:"addFolder.json",
					type:"post",
					dataType:"json",
					success:function(data){
						if(data.success){
							loadFiles($('#absolutePath').val());
							$('#folder_edit_dialog').dialog('close');
						}else{
							alert(data.message);
							$('#folder_edit_dialog').dialog('close');
						}
					}
				});
			},'取消':function(){
				$('#folder_edit_dialog').dialog('close');
			}
		}
	});
	 
}
function onNodeCreated(event, treeId, treeNode) {
	var current = $("#absolutePath").val();
	if (current == "/") {
		current = "";
	}
	$("#path_" + treeNode.id).val(current + "/" + treeNode.name);
}
function onRename(event, treeId, treeNode) {
	var current = $("#absolutePath").val();
	if (current == "/") {
		current = "";
	}
	$("#path_" + treeNode.id).val(current + "/" + treeNode.name);
}
function edit() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0];
	if (nodes.length == 0) {
		alert("请选择一个文件夹");
		return;
	}
	zTree.editName(treeNode);
}
 
function onClick(event, treeId, node, clickFlag) {
	loadFiles(node.path);
}
function clearChilds(e) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0];
	if (nodes.length == 0 || !nodes[0].isParent) {
		alert("Please select a parent folder");
		return;
	}
	zTree.removeChilds(treeNode);
}
function loadFolders() {
	$.ajax({url:"../ftp/folders.json", type:"get", dataType:"json", success:function (data) {
		if (data.success) {
			root = data.result;
			 
			$("#folder_add").bind("click", {isParent:true}, add);
			$("#edit").bind("click", edit);
			$("#clearChilds").bind("click", clearChilds);
			treeInitComplete = true;
			loadFiles("/");
			disabeldForward();
		} else {
			alert(data.message);
		}
	}});
}
function disabeldForward() {
	$("#action_forward").css("opacity", 0.4);
	$("#action_forward").attr("disabled", true);
}
function enabeldForward() {
	$("#action_forward").css("opacity", 1);
	$("#action_forward").attr("disabled", false);
}
function turnSize(size) {
	if (size > 1024) {
		size = size / 1024;
		size = Math.round(size * 100) / 100;
		size = size + " MB";
	} else {
		size = size + " KB";
	}
	return size;
}
function loadFiles(path) {
	initUploadButton(path);
	var folders=path.split('/');
	var str="<a dir='/'>根目录</a><i>»</i>";
	if(path!="/" && folders.length>1){
		for(var i=1;i<folders.length;i++){
			var dir=path.substring(0,path.indexOf(folders[i])+folders[i].length);
			str+="<a dir='"+dir+"'>"+folders[i]+"</a>";
		}
	}
	$('#navPath').html(str+"<em></em>");;
	$('#navPath a').each(function(){
		var dir=$(this).attr('dir');
		$(this).click(function(){
			loadFiles(dir);
		});
	});
	$("#viewmessageSubject").html("<img src='../img/ajax-loading.gif'/>Loading...");
	$("#absolutePath").val(path);
	$("#actionForm").ajaxSubmit({url:"../ftp/list.json", type:"post", dataType:"json", success:function (data) {
		if (data.success) {
			var files = data.result;
			$("#viewmessageSubject").html(path);
			var html = "";
			for (var i = 0; i < files.length; i++) {
				var ext = files[i].ext;
				if (ext) {
					ext = ext.toLowerCase();
				} else {
					ext = "folder";
				}
				html += "<li class='file' isFolder='" + files[i].isDirectory + "'   onclick='openFile(this)'><i class='mm_file " + ext + "'></i><a href='#' >" + files[i].name + "</a></li>";
			 
			}
			$("#explorerBody").html(html);
			
			
		} else {
			alert(data.message);
		}
	}});
}
function openFile(obj) {
	var $li=$(obj);
	var isFolder = $(obj).attr("isFolder");
	var path = $("#absolutePath").val();
	var file;
	if (path != "/") {
		file = path + "/" +$li.find('a').text();
	} else {
		file = "/" + $li.find('a').text();
	}

	if (isFolder == "true") {
		loadFiles(file);
		history = path;
	} else {
		downloadFile(file);
		$("#absolutePath").val(path);
	}
}
function downloadFile(file) {
	var uri = encodeURI("../ftp/download.json?bean.absolutePath=" + file);
	location.href = uri;
}

